package cn.hxzy.course.controller;


import cn.hxzy.common.domain.Courseware;
import cn.hxzy.common.domain.Video;
import cn.hxzy.common.response.R;
import cn.hxzy.common.response.StatusEnum;
import cn.hxzy.course.service.VideoService;
import cn.hxzy.common.util.AliOssUtil;
import com.aliyun.oss.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.jacoco.agent.rt.internal_43f5073.core.runtime.AgentOptions.OutputMode.file;

/**
 *
 * @author niuyi
 * @since 2024-06-22
 */
@RestController
@RequestMapping("/video")
@CrossOrigin
public class VideoController {
    @Autowired
    public VideoService videoService;


    //根据小节ID查看视频
    @GetMapping("/selectVideoList/{sectionId}")
    public R<List<Video>> selectVideoList(@PathVariable Integer sectionId) {
        return R.success(videoService.selectVideoList(sectionId));

    }
    //新增小节的视频
    @PostMapping("/saveVideo")
    public R saveVideo(@RequestBody Video video) {
        boolean save = videoService.save(video);
        if (save){
            return R.success(save);
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }

    /**
     * 搜索框搜索视频
     * @param videoName
     * @return
     */
    @GetMapping("/selectVideo/{videoName}/{courseId}")
    public R<List<Video>> selectVideo(@PathVariable("videoName") String videoName,@PathVariable("courseId") Long courseId) {
        List<Video> videos = videoService.selectVideo(videoName,courseId);
        return R.success(videos);
    }

    //根据课件编号删除视频
    @DeleteMapping("/deleteVideo")
    public R deleteVideo(@RequestParam("videoId") Integer videoId) {
        videoService.deleteVideo(videoId);
        return R.success("删除成功");
    }




    //新增视频
    @PostMapping("insertVideo")
    public R insert(MultipartFile file ) throws Exception {
        return R.success("");

    }


    @GetMapping("/getVideoTime")
    public R getVideoTime(@RequestParam("courseId") Integer courseId) {
        return R.success(videoService.getVideoTime(courseId));
    }

    @GetMapping("/getVideoNum")
    public R getVideoNum(@RequestParam("courseId") Long courseId) {
        return R.success(videoService.selectCount(courseId));
    }

    @GetMapping("/selectVideoJd")
    public R selectJd(@RequestParam Integer videoId){
        return R.success(videoService.selectJd( videoId));
    }

    @PostMapping("/updateVideoJd")
    public R updateJd(@RequestParam Integer videoId){
        return R.success(videoService.updateJd(videoId));
    }

    @PostMapping("/insertRedis")
    public R getVideoNum(@RequestParam Long videoId,@RequestParam Integer videoJd) {
        videoService.saveUserIdToRedis(videoId,videoJd);
        return R.success(200);
    }

    @GetMapping("/getVideoJd/{userId}/{videoId}")
    public R getVideoJd(@PathVariable("userId") long userId,@PathVariable("videoId") Integer videoId) {
        return R.success(videoService.selectVideoJd( userId,videoId));
    }

    @GetMapping("/getVideoPath")
    public R getVideoPath(@RequestParam("videoId") Integer videoId) {
        return R.success(videoService.getVideoPath(videoId));
    }

}

